/**
 * 聊天室逻辑
 */
var page = {
	init: function (data) {
		debugger
		var options = {
			appKey: data.appKey,
			account: data.account,
			id:data.chatroomId,
			token: data.token,
			chatroomNick: util.readCookie("nickName"),
			chatroomAvatar: util.readCookie("avatar"),
			address:data.address
		}
		this.roomData = JSON.parse( sessionStorage.getItem('roomData') );
		this.roomId =data.chatroomId;
		this.account = data.account;
		this.offsetTime = 0;
		this.liveUrl = sessionStorage.getItem('liveUrl');
		this.m3u8Url  = this.liveUrl.split(",")[0];
		this.rtmpUrl = this.liveUrl.split(",")[1];
		$("#my-video").show();
		netcall = Netcall.getInstance({
			nim: nim,
			container: document.getElementById('video'),
			
			// 是否开启日志打印
			debug: true
		});


		this.initNode();
		this.initEmoji();
		this.initEvt();
		var link = this.link = window.link = new LinkRoom(this, options);
		this.data = link.data;
		this.initVideo();
		this.getWeath();
		this.neplayer;

	},
	//sdk连接后初始化页面
	initReady: function (data) {
		var chatroom = data.chatroom,
			member = data.member,
			total,
			that = this;
		$("#linkStatus").addClass("hide");
		//聊天室信息
		$("#roomTitle").text(chatroom.name);
		this.link.getChatroomMembersInfo([chatroom.creator], function (err, item) {
			// 缓存起来说不定以后要用
			if (!that.data.person[item.members[0].account]) {
				that.data.person[item.members[0].account] = item.members[0];
			}
			$("#roomCreator").text(item.members[0].nick);
			$("#roomAvatar")[0].src = util.getAvatar(item.members[0].avatar);
		})
		if (chatroom.onlineMemberNum > 10000) {
			var value = new Number(chatroom.onlineMemberNum / 10000);
			total = value.toFixed(1) + "万";
		} else {
			total = chatroom.onlineMemberNum;
		}
		$("#roomOnlineMemberNum").text(total);
		$("#roomAnnouncement").text(chatroom.announcement || "暂无公告");
		// 设置我的权限
		this.setMyRole();
	},
	initNode: function () {
		this.$tab = $("#room .j-tab");
		this.$pannel = $("#room .j-pannel");
		this.$customBtn = $("#customBtn");
		this.$chat = $("#room .j-chat");
		this.$member = $("#room .j-member");
		this.$more = $("#room .j-more");
		this.$menu = $("#menu");
		this.$showEmoji = $("#emoji");
		this.$sendText = $("#sentText");
		this.$editText = $("#editText");
		//连麦
		this.$sendWheat = $("#sentWheat");
		//测试btn
		this.$btn = $("#btn")
		
	},
	initEvt: function () {
		var that = this;
		this.$tab.on("click", this.switchTab.bind(this));
		this.$sendText.on("click", this.sendText.bind(this));
		this.$editText.keydown(function (e) {
			if (e.ctrlKey && e.keyCode == 13) {
				that.sendText();
			}
		});
		this.$more.on("click", this.loadMoreMembers.bind(this));
		this.$customBtn.on("click", this.showLaowang.bind(this));
		this.$chat.delegate(".j-nick", "click", this.showMenu.bind(this));
		this.$menu.mouseleave(this.hideMenu.bind(this));
		this.$menu.delegate(".j-item", "click", this.dealCommand.bind(this));
		this.$showEmoji.on('click', this.showEmoji.bind(this));

		this.$sendWheat.on("click", this.sendWheat.bind(this));
		//测试按钮
		this.$btn.on("click",this.playerVideo.bind(this));
	},
	playerVideo:function() {
		console.log(1)
		this.neplayer.play();
		var version = this.neplayer.getVersion();
		console.log(version)
	},
	//目前图片写死代替
	initVideo: function () {
		$("#video")[0].src = "./images/image" + util.getIdTag() + ".jpg";
		$("#video").removeClass("hide");
		//$("#my_source").attr('src',this.liveUrl);
		//$("#my-video").attr('src',this.liveUrl);
		var options = {
			"controls": true, //是否显示控制条
			"autoplay":!0, //是否自动播放(ios不支持自动播放)
			"errMsg7": "直播已结束",
			/*预加载选项*/
			//"preload": "auto",
			/*
			'auto'预加载视频（需要浏览器允许）;
			'metadata'仅预加载视频meta信息;
			'none'不预加载;
			*/
			"techOrder": ["flash", "html5"], //优先使用的播放模式
			"streamTimeoutTime": 30 * 1000, //拉流超时时间，默认30s
			/*设置播放器控件*/
			controlBar: { 
				playToggle: true
			}
		}
		window.neplayer.setSWF("./dist/js/video-js.swf");
		var that = this;
		this.neplayer = window.neplayer('my-video', options, function(res){
			
			// http://218.108.32.134:8911/SktApp/5985f96bfd446c759/play.m3u8
			// rtmp://218.108.32.134:1935/live/yulong,rtmp://218.108.32.134:1935/live/cy123
			that.neplayer.setDataSource([
				//{type: "video/x-flv",src: "http://www.example.com/path/to/video.flv"},
				{type: "rtmp/flv",src: that.rtmpUrl},
				{type: "application/x-mpegURL",src: that.m3u8Url}//m3u8
				//{type: "rtmp/flv",src: 'rtmp://218.108.32.134:1935/live/cy123'},//可以播放
				
				
            ]);
            $("#my-video").show();
		});

	},
	initEmoji: function () {
		var that = this,
			emojiConfig = {
				'emojiList': emojiList,  //普通表情
				'width': 350,
				'height': 300,
				'imgpath': './images/',
				'callback': function (result) {
					that.cbShowEmoji(result);
				}
			}
		this.$emNode = new CEmojiEngine($('#emojiTag')[0], emojiConfig);
	},
	getWeath: function (params) {

	},
	setMyRole: function () {
		var member = this.data.person[this.account];
		if (member.type === 'owner') {
			this.$chat.addClass("owner");
			this.$menu.addClass("owner");
			this.myRole = 3;
		} else if (member.type === 'manager') {
			this.myRole = 2;
			this.$chat.addClass("manager");
		} else {
			this.myRole = 1;
			this.$chat.removeClass("manager");
		}
	},
	//切换tab
	switchTab: function (evt) {
		if ($(evt.target).attr("data-value") !== $("#room .j-tab.crt").attr("data-value")) {
			//点击在线成员重新拉下名单
			if ($(evt.target).attr("data-value") === "member") {
				this.link.getChatroomMembers(false, 0, 20, this.cbGetChatroomMembers.bind(this));
			} else {
				this.$pannel.toggleClass("hide");
				this.$tab.toggleClass("crt");
			}

		}
	},
	// 拉取固定成员回调
	cbGetChatroomMembers: function (err, data) {
		if (!err) {
			// 渲染列表
			this.buildChatMember(data.members);
		} else {
			alert(err);
		}
	},
	//发送自定义消息（定义好data格式与解析方式，实现自定义的消息比如猜拳，送花等）
	showLaowang: function () {
		var content = {
			type: 1,
			data: {
				value: Math.ceil(Math.random() * 3)
			}
		};
		this.link.sendCustomMessage(content, function (err, data) {
			if (err) {
				alert(err.message);
			} else {
				this.buildChat([data], "msgs");
			}
		}.bind(this))
	},
	// 操作菜单
	showMenu: function (evt) {
		var that = this,
			info,
			type = evt.currentTarget.getAttribute("data-role"),
			ownerOperate = (that.myRole === 3 && type !== "owner"),
			account = that.crtAccount = evt.currentTarget.getAttribute("data-account");
		managerOperate = (that.myRole === 2 && type !== "owner");
		if (account === this.account) {
			return;
		}
		var show = function (item) {
			if (item.account) {
				that.$menu.find(".j-gag").text(item.gaged ? "解除禁言" : "禁言");
				that.$menu.find(".j-black").text(item.blacked ? "解除拉黑" : "拉黑该用户");
				that.$menu.find(".j-admin").text(item.type === "manager" ? "解除管理员" : "任命管理员");
			}
			if (!ownerOperate && item.type && item.type === "manager") {
				return;
			}
			if (ownerOperate || managerOperate) {
				var x = evt.clientX + (window.scrollX || window.pageXOffset),
					y = evt.clientY + (window.scrollY || window.pageYOffset);
				that.$menu.css({ "top": y - 10, "left": x - 10, "display": "block" });
			}
		}
		if (this.data.person[account]) {
			info = this.data.person[account];
			show(info);
		} else {
			this.link.getChatroomMembersInfo([account], function (err, data) {
				if (!err) {
					var member;
					if (data.members.length) {
						info = that.data.person[account] = data.members[0];
						show(info)
					} else {
						info = that.data.person[account] = {};
						show(info);
					}
				} else {
					alert(err);
				}
			})
		}
	},
	hideMenu: function () {
		this.crtAccount = null;
		this.$menu.css("display", "none");
		this.$menu.find(".j-gag").text("禁言");
		this.$menu.find(".j-black").text("拉黑该用户");
		this.$menu.find(".j-admin").text("任命管理员");
	},
	dealCommand: function (evt) {
		var type = evt.target.getAttribute("data-type"),
			that = this,
			account = this.crtAccount;
		if (account) {
			this.link.dealCommand(type, account, this.data.person[account]);
		}
		this.crtAccount = null;
		that.$menu.css("display", "none");
	},
	showEmoji: function () {
		this.$emNode._$show();
	},
	cbShowEmoji: function (data) {
		this.$editText.val(this.$editText.val() + data.emoji);
	},
	sendText: function () {
		var text = this.$editText.val().trim();
		var that = this;
		if (text.length > 500) {
			alert('消息长度最大为500字符');
		} else if (text.length === 0) {
			return;
		} else {
            this.link.sendText(text, function (err, data) {
                if (err) {
                    alert(err.message);
                } else {
                    this.$editText.val("");
                    this.buildChat([data], "msgs");
                }
            }.bind(this))
			
		}
    },
	sendWheat: function () {
		var that = this;
		var status = this.$sendWheat.html();
		if(status == "连麦"){
			/* var content = {
				type: '1',
				value: {"command":"1","roomid":that.roomid} 
			}; */
			var content =  {"command":"1","roomid":that.roomId }

			content = JSON.stringify(content);
			var msgId = nim.sendCustomSysMsg({
				scene: 'p2p',
				to: that.roomData.wyy_uuid,
				content: content,
				sendToOnlineUsersOnly: false,
				apnsText: content,
				done: sendCustomSysMsgDone
			});
			console.log('正在发送p2p自定义系统通知, id=' + msgId);
			function sendCustomSysMsgDone(error, msg) {
				console.log('发送' + msg.scene + '自定义系统通知' + (!error?'成功':'失败') + ', id=' + msg.idClient);
				console.log(error);
				console.log(msg);
			}

			netcall.on('joinChannel', function (obj) {
				// 通知上层有其他用户加入了房间，上层做相应逻辑和UI处理
				// 这里可以预览加入的同学的视频流
				// 播放对方声音
				netcall.startDevice({
				type: Netcall.DEVICE_TYPE_AUDIO_OUT_CHAT
				}).catch(function() {
				console.log('播放对方的声音失败')
				})
			
				// 预览加入的同学的视频流
				netcall.startRemoteStream({
				account: obj.account,
				node: document.getElementById('video1')
				})
			
				// 设置对方预览画面大小
				netcall.setVideoViewRemoteSize({
				account:obj.account,
				with: 100,
				height: 100,
				cut:true
				})
			});
			//离开房间通知
			netcall.on('leaveChannel', function (obj) {
				// 通知上层有其他用户离开了会议，上层做相应逻辑和UI处理

				// 停止预览该同伴的视频流
				netcall.stopRemoteStream({
					account: obj.account
				})
			})
			/*
			var teacherName = 'testChannelName' + parseInt(Math.random() * 10000)
			
			//加入房间通知
			netcall.joinChannel({
				channelName: teacherName, //必填
				type: Netcall.NETCALL_TYPE_VIDEO,
				liveEnable: true, //开启互动直播
				sessionConfig: sessionConfig
			}).then(function (obj) {
				// 加入房间成功后的上层逻辑操作
				// eg: 开启摄像头
				// eg: 开启麦克风
				// eg: 开启本地流
				// eg: 设置音量采集、播放
				// eg: 设置视频画面尺寸等等，具体请参照p2p呼叫模式
				console.log(obj)
				

				
				// 通知上层有其他用户加入了房间，上层做相应逻辑和UI处理
				// 这里可以预览加入的同学的视频流
				// 播放对方声音
				netcall.startDevice({
					type: Netcall.DEVICE_TYPE_AUDIO_OUT_CHAT
				}).catch(function () {
					console.log('播放对方的声音失败')
				})

				// 预览加入的同学的视频流
				netcall.startRemoteStream({
					account: obj.account,
					node: document.getElementById('remoteContainer1')
				})

				// 设置对方预览画面大小
				netcall.setVideoViewRemoteSize({
					account:  obj.account,
					with: 500,
					height: 500,
					cut: true
				})
			})
			//离开房间通知
			netcall.on('leaveChannel', function (obj) {
				// 通知上层有其他用户离开了会议，上层做相应逻辑和UI处理

				// 停止预览该同伴的视频流
				netcall.stopRemoteStream({
					account: obj.account
				})
			})*/
		}else if(status == "挂断"){
			netcall.leaveChannel().then(function(obj) {
				// 离开房间后，开发者自己业务的扫尾工作
				this.$sendWheat.html('连麦');
				netcall.stopDevice(Netcall.DEVICE_TYPE_VIDEO)	
				netcall.stopDevice(Netcall.DEVICE_TYPE_AUDIO_IN)
			});
		}
		
	},
	// 聊天页面绘制
	buildChat: function (data, type) {
		var html = "",
			item,
            prepend = false;

		data.sort(function (a, b) {
			return a.time - b.time;
		});
		if (type === "msgs") {
			for (var i = 0; i < data.length; i++) {
				item = data[i];
				if (this.$chat.find('.item[data-id="' + item.idClient + '"]').length) {
					continue;
				}
				if (item.type !== "notification") {
					html += this.buildMsgUI(item);
				} else {
					//对于系统通知，更新下用户信息的状态
					if (item.attach.type === "blackMember" || item.attach.type === "unblackMember" || item.attach.type === "gagMember" || item.attach.type === "ungagMember" || item.attach.type === "addManager" || item.attach.type === "removeManager") {
						this.updatePersonInfo(item.attach.to[0]);
					}
					html += this.buildSysMsgUI(item);
				}
			}
			this.$chat.append(html);
		} else {
			// 历史消息
			for (var i = 0; i < data.length; i++) {
				item = data[i];
				if (this.$chat.find('.item[data-id="' + item.idClient + '"]').length) {
					continue;
				}
				if (item.type !== "notification") {
					html += this.buildMsgUI(item);
				} else {
					html += this.buildSysMsgUI(item);
				}
			}
			this.$chat.prepend(html);
		}
		//这里可以做是否自动滚动的效果 
		this.scrollToBottom();
	},
	//消息UI绘制
	buildMsgUI: function (item) {
		var info = item.custom ? JSON.parse(item.custom) : {},
			type = util.parseMemberTypeToString(info.type),
			role = type === "owner" || type === "manager",
			roleClass = role ? "roles" : "";
		return ['<div class="item" data-id="' + item.idClient + '">',
		'<p class="nick ' + type + ' j-nick ' + roleClass + '" data-role="' + type + '" data-account="' + item.from + '">' + ((type === "owner" || type === "manager") ? '<b class="role role-' + type + '"></b>' : "") + item.fromNick + '</p>',
		'<p class="text">' + ( util.buildMsg(item) || '') + '</p>',
			'</div>'].join("");
	},
	//系统消息UI绘制
	buildSysMsgUI: function (item) {
		return ['<div class="item" data-id="' + item.idClient + '">',
		'<p class="text sys">' + util.buildSysMsg(item) + '</p>',
			'</div>'].join("");
	},
	//成员列表UI绘制
	buildChatMemberUI: function (data) {
		var type = data.type;
		return ['<div class="item" data-id="' + data.account + '">',
			'<div class="avatar">',
		'<img src="' + util.getAvatar(data.avatar) + '"/>',
			'</div>',
		'<div class="nick">' + data.nick + '</div>',
		(type === "owner" || type === "manager") ? '<b class="role role-' + type + '"></b>' : '',
			'</div>'].join("");
	},
	//显示在线成员逻辑
	buildChatMember: function (data) {
		this.memberCount = 0;
		this.offsetTime = 0;
		this.loadGuest = false;
		var that = this;
		//列表数据，是否是游客
		this.$member.html(this.buildFixedMemberList(data));
		//有数据在切换列表
		this.$pannel.toggleClass("hide");
		this.$tab.toggleClass("crt");

		if (this.memberCount < 20) {
			this.nextLoadGuest();
		}
	},
	nextLoadGuest: function () {
		var that = this;
		this.loadGuest = true;
		this.offsetTime = 0;
		this.link.getChatroomMembers(true, this.offsetTime, 20 - this.memberCount, function (err, data) {
			if (!err) {
				var members = data.members;
				if (members.length < (20 - that.memberCount)) {
					$(".j-more").addClass("hide");
				} else {
					$(".j-more").removeClass("hide");
				}
				that.$member.append(that.bulidGuestList(members));
			} else {
				alert(err);
			}
		});
	},
	//渲染固定成员列表
	buildFixedMemberList: function (data) {
		var html = "",
			html1 = "",
			html2 = "",
			html3 = "",
			temp,
			item;
		for (var i = 0; i < data.length; i++) {
			if (i === 0) {
				this.offsetTime = data[i].updateTime;
			}
			item = data[i];
			this.data.person[item.account] = item;
			if (item.type === "owner") {
				html += this.buildChatMemberUI(item);
			} else if (item.type === "manager") {
				html1 += this.buildChatMemberUI(item);
			} else if (item.type === "common") {
				html2 += this.buildChatMemberUI(item);
			} else {
				html3 += this.buildChatMemberUI(item);
			}
			this.memberCount++;
		}
		temp = '<div class="j-ownerList">' + html + '</div>' + '<div class="j-managerList">' + html1 + '</div>' + '<div class="j-normalList">' + html2 + '</div>' + '<div class="j-otherList">' + html3 + '</div>';
		return temp;
	},
	// 渲染游客列表
	bulidGuestList: function (data) {
		var html = "",
			item,
			temp;
		for (var i = 0; i < data.length; i++) {
			if (i === data.length - 1) {
				this.offsetTime = data[i].enterTime;
			}
			item = data[i];
			this.data.person[item.account] = item;
			html += this.buildChatMemberUI(item);
			this.memberCount++;
		}
		temp = '<div class="j-guestList">' + html + '</div>'
		return temp;
	},
	// 更新成员信息
	updatePersonInfo: function (account) {
		var that = this;
		this.link.getChatroomMembersInfo([account], function (err, data) {
			if (!err) {
				var member;
				if (data.members.length) {
					that.data.person[account] = data.members[0];
				} else {
					that.data.person[account] = {};
				}
				if (that.account === account) {
					that.setMyRole();
				}
			} else {
				alert(err);
			}
		})
	},
	//更新聊天室成员UI（通知消息进入出去）（干掉了，根据消息去画不如点击重新去拉）
	// updateMemberUI:function(account,type){	
	// 	var that = this;
	// 	if(type==="in"){
	// 		if(!this.data.person[account]){
	// 			this.link.getChatroomMembersInfo([account],function(err,items){
	// 				// 缓存起来说不定以后要用
	// 				if(!err){
	// 					that.data.person[items.members[0].account] = items.members[0];
	// 					var item = that.$member.find(".item[data-id="+account+"]");
	// 					if(item.length){
	// 						item.removeClass("hide");
	// 					}else{
	// 						that.$member.append(that.buildChatMemberUI(items.members[0]));
	// 					}			
	// 				}else{
	// 					alert(err);
	// 				}
	// 			})		
	// 		}else{
	// 			var item = that.$member.find(".item[data-id="+account+"]");
	// 			if(item.length){
	// 					item.removeClass("hide");
	// 			}else{
	// 				that.$member.append(that.buildChatMemberUI(this.data.person[account]));
	// 			}
	// 		}
	// 	}else{
	// 		that.$member.find(".item[data-id="+account+"]").addClass("hide");
	// 	}	
	// },

	// 加载跟多成员
	loadMoreMembers: function () {
		this.link.getChatroomMembers(this.loadGuest, this.offsetTime, 20, this.cbLoadMoreMembers.bind(this));
	},
	cbLoadMoreMembers: function (err, data) {
		if (!err) {
			var html = "",
				members = data.members,
				$ownerList = this.$member.find(".j-ownerList"),
				$managerList = this.$member.find(".j-managerList"),
				$normalList = this.$member.find(".j-normalList"),
				$otherList = this.$member.find(".j-otherList"),
				$guestList = this.$member.find(".j-guestList"),
				item;
			for (var i = 0; i < members.length; i++) {
				if (i === members.length - 1) {
					this.offsetTime = members[i].enterTime;
				}
				item = members[i];
				this.data.person[item.account] = item;
				if (item.type === "owner") {
					$ownerList.append(this.buildChatMemberUI(item));
				} else if (item.type === "manager") {
					$managerList.append(this.buildChatMemberUI(item));
				} else if (item.type === "common") {
					$normalList.append(this.buildChatMemberUI(item));
				} else {
					$guestList.append(this.buildChatMemberUI(item));
				}
			}
			if (members.length < 20) {
				if (!this.loadGuest) {
					this.nextLoadGuest();
				} else {
					$(".j-more").addClass("hide");
				}
			} else {
				$(".j-more").removeClass("hide");
			}
		} else {
			alert(err);
		}
	},
	// 来消息是否滚动 可以设置一个开关在这个函数实现逻辑
	scrollToBottom: function () {
		if (true) {
			this.$chat.scrollTop(99999);
		}
	}
};
